//
//  ViewController.m
//  下拉图片变大,导航栏渐变
//
//  Created by dllo on 16/8/17.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#define kTableViewReuse @"reuse"
#define kWidth self.view.bounds.size.width
#define kHeight self.view.bounds.size.height
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *backViewForTableHeaderView;
@property (nonatomic, strong) UIImageView *backImageViewForTableHeaderView;
@property (nonatomic, strong) UIImageView *barImageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.barImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, kWidth, 64)];
    _barImageView.image = [UIImage imageNamed:@"navbar"];
    
    [self.navigationController.navigationBar addSubview:_barImageView];
    self.navigationItem.title = @"这是一个导航栏";
    [self createTableView];
}
- (void)createTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, kWidth, kHeight + 64)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewReuse];
    _tableView.tableHeaderView = [self createTableHeaderView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    cell.textLabel.text = [NSString stringWithFormat:@"cell--%ld", indexPath.row];
    return cell;
}
- (UIView *)createTableHeaderView {
    self.backViewForTableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    self.backImageViewForTableHeaderView = [[UIImageView alloc] initWithFrame:self.backViewForTableHeaderView.bounds];
    _backImageViewForTableHeaderView.image = [UIImage imageNamed:@"1"];
    [self.backViewForTableHeaderView addSubview:_backImageViewForTableHeaderView];
    
    return self.backViewForTableHeaderView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offset_Y = scrollView.contentOffset.y;
    CGFloat alpha = (offset_Y + 64) / 200.0f;
    self.barImageView.alpha = alpha;
    NSLog(@"%lf", offset_Y);
    if (offset_Y < -64) {
        CGFloat add_height = - (offset_Y + 64);
        CGFloat scale = (200 + add_height) / 200.0f;
        self.backImageViewForTableHeaderView.frame = CGRectMake(- (kWidth * scale - kWidth) / 2, - add_height, kWidth * scale, 200 + add_height);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
